﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JiuMeiMall.PMethod
{
    public static class Pager
    {
        /// <summary>
        /// 获取分页字符串
        /// </summary>
        /// <param name="page">PageParameters对象</param>
        /// <returns></returns>
        public static string GetPageStr(PageParameters page)
        {
            int SpanMark = 7;//分页显示长度
            int AllPageCount = GetAllPageCount(page.RecordCount, page.PageSize);
            return SetPageString(page, SpanMark, AllPageCount);
        }
        /// <summary>
        /// 设置分页字符串
        /// </summary>
        /// <param name="page">PageParameters对象</param>
        /// <param name="SpanMark">标识</param>
        /// <param name="AllPageCount">总页数</param>
        /// <returns></returns>
        private static string SetPageString(PageParameters page, int SpanMark, int AllPageCount)
        {
            StringBuilder sb_page = new StringBuilder();
            if (page.Pageindex == 1)
            {
                sb_page.AppendFormat("<span class=\"page_a up_page\">上一页</span>");
            }
            else
            {
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\"page_a up_page\">上一页</a>", (page.Pageindex - 1));
            }
            if (page.Pageindex < SpanMark || AllPageCount == SpanMark)
            {
                int tempPage = 7;
                if (AllPageCount <= 7)
                {
                    tempPage = AllPageCount;
                }
                for (int i = 1; i <= tempPage; i++)
                {
                    if (page.Pageindex == i)
                    {
                        sb_page.AppendFormat("<a class=\" page_a current\">{0}</a>", i);
                    }
                    else
                    {
                        sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", i);
                    }
                }
                if (AllPageCount > 7)
                {
                    sb_page.AppendFormat("<a href=\"#\" class=\"page_a\">...</a>");
                    //int tempPage2 = AllPageCount - 1;
                    //if ((AllPageCount - page.Pageindex) > 1)
                    //{
                    //    sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", tempPage2);
                    //}
                    sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", AllPageCount);
                }
            }
            else if ((AllPageCount - page.Pageindex) < 6)
            {
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex=1" + page.Parameters + "\" class=\" page_a\">1</a>");
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex=2" + page.Parameters + "\" class=\" page_a\">2</a>");
                sb_page.AppendFormat("<span class=\"page_a\">...</span>");
                for (int i = AllPageCount - 5; i <= AllPageCount; i++)
                {
                    if (page.Pageindex == i)
                    {
                        sb_page.AppendFormat("<a class=\" page_a current\">{0}</a>", i);
                    }
                    else
                    {
                        sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", i);
                    }
                }
            }
            else
            {
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex=1" + page.Parameters + "\" class=\" page_a\">1</a>");
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex=2" + page.Parameters + "\" class=\" page_a\">2</a>");
                sb_page.AppendFormat("<span class=\"page_a\">...</span>");
                for (int i = page.Pageindex - 3; i < page.Pageindex + 2; i++)
                {
                    if (page.Pageindex == i)
                    {
                        sb_page.AppendFormat("<a class=\" page_a current\">{0}</a>", i);
                    }
                    else
                    {
                        sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", i);
                    }
                }
                sb_page.AppendFormat("<span class=\"page_a\">...</span>");
                int tempPage = AllPageCount - 1;
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", tempPage);
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\" page_a\">{0}</a>", AllPageCount);
            }
            if (page.Pageindex == AllPageCount)
            {
                sb_page.AppendFormat("<span class=\"page_a down_page\">下一页</span>");
            }
            else
            {
                sb_page.AppendFormat("<a href=\"" + page.Url + "?pageindex={0}" + page.Parameters + "\" class=\"page_a down_page\">下一页</a>", (page.Pageindex + 1));
            }
            string location = page.Url + "|" + AllPageCount + "|" + page.Parameters;
            sb_page.AppendFormat("<a href=\"javascript:gopage('" + location + "')\" class=\"go_btn image_url flr mr5\">GO</a>");
            sb_page.AppendFormat("<input type=\"text\" class=\"page_input\" id=\"gopage\" name=\"gopage\" />");
            return sb_page.ToString();
        }
        /// <summary>
        /// 计算总页数
        /// </summary>
        /// <returns></returns>
        private static int GetAllPageCount(int recordcount, int pagesize)
        {
            int AllPageCount = (int)Math.Ceiling(Convert.ToDouble(recordcount) / Convert.ToDouble(pagesize));
            if (AllPageCount == 0)
            {
                AllPageCount = 1;
            }
            return AllPageCount;
        }
    }
    /// <summary>
    /// 分页参数
    /// </summary>
    public class PageParameters
    {
        /// <summary>
        /// 总数据量
        /// </summary>
        private int recordCount;

        public int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }
        /// <summary>
        /// 页大小
        /// </summary>
        private int pageSize;

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        /// <summary>
        /// 当前页码
        /// </summary>
        private int pageIndex;

        public int Pageindex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }
        /// <summary>
        /// 链接地址
        /// </summary>
        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }
        /// <summary>
        /// 参数字符串
        /// </summary>
        private string parameters;

        public string Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }
    }
}